package com.zzzy.server.security;

import com.zzzy.common.enums.ResultCodeEnum;
import com.zzzy.common.util.JsonUtil;
import com.zzzy.common.util.ServletUtils;
import com.zzzy.common.vo.AjaxResult;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;

/**
 * Description: 认证失败处理类 返回未授权
 *
 * @author fc
 * @date 2024/4/12
 */
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint, Serializable {
    private static final long serialVersionUID = -6890854296954588122L;

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) {
        ServletUtils.renderString(response, JsonUtil.toJSONString(AjaxResult.build(ResultCodeEnum.FORBIDDEN.getCode(), "认证失败，未登录或登录状态已过期，请重新登录后重试")));
    }
}
